﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;

namespace Xceed.Silverlight.Data.Stats
{
  public class CountFunction : CumulativeStatFunction
  {
    public CountFunction()
      : base()
    {
    }

    public CountFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    public CountFunction( string resultPropertyName, string sourcePropertyName, bool includeNullValues )
      : base( resultPropertyName, sourcePropertyName )
    {
      m_includeNullValues = includeNullValues;
    }

    #region IncludeNullValues Property

    public bool IncludeNullValues
    {
      get
      {
        return m_includeNullValues;
      }
      set
      {
        this.CheckSealed();
        m_includeNullValues = value;
      }
    }

    private bool m_includeNullValues = false;

    #endregion

    protected internal override void InitializeFromInstance( StatFunction source )
    {
      base.InitializeFromInstance( source );

      this.IncludeNullValues = ( ( CountFunction )source ).IncludeNullValues;
    }

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected internal override void Reset()
    {
      m_count = 0;
    }

    protected internal override void Accumulate( object[] values )
    {
      if( values.Length > 0 )
      {
        if( ( this.IncludeNullValues ) || ( ( values[ 0 ] != null ) && !( values[ 0 ] is DBNull ) ) )
        {
          checked
          {
            m_count++;
          }
        }
      }
    }

    protected internal override void AccumulateChildResult( StatResult childResult )
    {
      checked
      {
        m_count += Convert.ToInt64( childResult.Value );
      }
    }

    protected internal override StatResult GetResult()
    {
      return new StatResult( m_count );
    }

    protected internal override bool IsEquivalent( StatFunction statFunction )
    {
      CountFunction countFunction = statFunction as CountFunction;

      if( ( countFunction == null ) || ( countFunction.IncludeNullValues != this.IncludeNullValues ) )
        return false;

      return base.IsEquivalent( statFunction );
    }

    protected internal override int GetEquivalenceKey()
    {
      return base.GetEquivalenceKey() ^ this.IncludeNullValues.GetHashCode();
    }

    private long m_count;
  }
}
